package com.hulizuofang.wol.chat.client.presenter;

import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.hulizuofang.wol.chat.client.action.SetLoginResult;
import com.hulizuofang.wol.chat.client.action.SetLoginResult.SetLoginResultWorker;
import com.hulizuofang.wol.chat.client.action.SetLoginAction;
import com.hulizuofang.wol.chat.client.place.NameTokens;
import com.hulizuofang.wol.chat.client.ui.event.changeplace.ChangePlaceEvent;
import com.hulizuofang.wol.chat.client.ui.event.selectuser.SelectUserEvent;
import com.hulizuofang.wol.chat.client.ui.event.selectuser.SelectUserHandler;
import com.hulizuofang.wol.chat.client.ui.event.whoisonline.UpdateWhoisOnlineEvent;
import com.hulizuofang.wol.chat.client.ui.event.whoisonline.WhoisOnlineHandler;
import com.hulizuofang.wol.chat.client.ui.user.ChatUser;

public class WhoisOnlinePagePresenter extends AbstractNestedPresenter<WhoisOnlinePagePresenter.PageView, WhoisOnlinePagePresenter.PageProxy> {
	private static final Logger logger = Logger.getLogger(WhoisOnlinePagePresenter.class.getName());
	
	@ProxyCodeSplit
	@NameToken(NameTokens.WHOIS_ONLINE)
	public interface PageProxy extends ProxyPlace<WhoisOnlinePagePresenter> {
	}

	public interface PageView extends View {
		void refreshOnlineUser(Set<ChatUser> users);
		void showUser(ChatUser user);
	}

	@Inject
	public WhoisOnlinePagePresenter(EventBus eventBus, PageView view, PageProxy proxy, DispatchAsync dispatcher) {
		super(eventBus, view, proxy, dispatcher);
	}

	private WhoisOnlineHandler whoisOnlineHandler = new WhoisOnlineHandler() {
		@Override
		public void onUpdateWhoisOnline(UpdateWhoisOnlineEvent event) {
			getView().refreshOnlineUser(event.getUsers());
		}
	};

	private SelectUserHandler selectUserHandler = new SelectUserHandler() {
		@Override
		public void onSelectUser(SelectUserEvent event) {
			
		}
	};
	
	@Override
	public void onBind() {
		getEventBus().addHandler(UpdateWhoisOnlineEvent.getType(), whoisOnlineHandler);
		getEventBus().addHandler(SelectUserEvent.getType(), selectUserHandler);
	}
	
	private AsyncCallback<SetLoginResult> callback = new AsyncCallback<SetLoginResult>() {
		@Override
		public void onFailure(Throwable caught) {
			Window.alert("Login fails !");
			logger.log(Level.SEVERE, "", caught);
			ChangePlaceEvent.fire(getEventBus(),new PlaceRequest(NameTokens.START).with(NameTokens.LOGIN, getLogin()));
		}
		
		@Override
		public void onSuccess(SetLoginResult result) {
			result.workResult(new SetLoginResultWorker() {
				@Override
				public void work(ChatUser user, Set<ChatUser> onlineUser) {
					getView().showUser(user);
					UpdateWhoisOnlineEvent.fire(getEventBus(), onlineUser);
				}
			});
		}
	};

	@Override
	protected void onReveal() {
		SetLoginAction action = new SetLoginAction(getLogin());
		getDispatcher().execute(action, callback);
	}
}
